/*
 * CastDRAndroid
 * Copyright (c) 2014 Christian Holm Christensen
 *
 * This program is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *
 */
package org.cholm.media.castdr.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import org.cholm.media.castdr.LogUtils;
import org.cholm.media.castdr.R;
import org.cholm.media.castdr.backend.LiveChannel;

import static org.cholm.media.castdr.LogUtils.LOGd;
import static org.cholm.media.castdr.LogUtils.LOGw;
/**
 * Adapter to show a live cannel. Note, this only serves one sub-view
 *
 * @author cholm
 */
public class LiveAdapter extends BaseAdapter {
    /**
     * Call-back on selection
     */
    private OnSelectionListener mListener = null;
    /**
     * Context
     */
    private Context mContext = null;
    /**
     * Our live channel
     */
    private LiveChannel mData = null;

    /**
     * Constructor
     *
     * @param ctx Context
     */
    public LiveAdapter(Context ctx) {
        mContext = ctx;
        mListener = null;
    }

    /**
     * Set the call-back on clicks
     *
     * @param l Listener
     */
    public void setListener(OnSelectionListener l) {
        mListener = l;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getCount()
     */
    @Override
    public int getCount() {
        // We have exactly 1 entry
        return mData == null ? 0 : 1;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getItem(int)
     */
    @Override
    public Object getItem(int position) {
        return mData;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getItemId(int)
     */
    @Override
    public long getItemId(int position) {
        return 0;
    }

    /*
     * (non-Javadoc)
     *
     * @see android.widget.Adapter#getView(int, android.view.View,
     * android.view.ViewGroup)
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LiveHolder holder = null;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.live_channel, null);
            holder = new LiveHolder(convertView);
        }
        if (holder == null) holder = (LiveHolder) convertView.getTag();
        holder.set(mData);

        return convertView;
    }

    /**
     * Set our data
     *
     * @param data The live channel object
     */
    public void setData(LiveChannel data) {
        mData = data;
    }

    public interface OnSelectionListener {
        public void onSelect(LiveChannel lc);
    }

    /**
     * Holder of view
     *
     * @author cholm
     */
    private class LiveHolder {
        /**
         * debug tag
         */
        private final String TAG = LogUtils.makeLogTag("LiveAdapter" +
                ".LiveHolder");
        /**
         * Top view
         */
        public View mTop = null;
        /**
         * title of the current show
         */
        public TextView mTitle = null;
        /**
         * Start date of the current show
         */
        public TextView mDate = null;
        /**
         * Description of the current show
         */
        public TextView mDesc = null;
        /**
         * Next program time
         */
        public TextView mNextDate = null;
        /**
         * Next program title
         */
        public TextView mNextTitle = null;

        LiveHolder(View v) {
            mTop = v;
            mTitle = (TextView) v.findViewById(R.id.title);
            mDate = (TextView) v.findViewById(R.id.date);
            mDesc = (TextView) v.findViewById(R.id.desc);
            mNextDate = (TextView) v.findViewById(R.id.nextDate);
            mNextTitle = (TextView) v.findViewById(R.id.nextTitle);

            mTop.setTag(this);
        }

        /**
         * Set the view from a channel object
         *
         * @param l Live channel object
         */
        public void set(LiveChannel l) {
            if (l == null) {
                LOGw(TAG, "No LiveChannel object passed to holder");
                return;
            }
            if (l.mNow == null) {
                LOGw(TAG, "No Now object in live channel");
                return;
            }
            if (l.mNext == null) {
                LOGw(TAG, "No Next object in live channel");
                return;
            }
            String d = l.mNow.mDesc;
            d = d.replaceAll("\\s", " ");
            mTitle.setText(l.mNow.mTitle);
            mDate.setText(l.mNow.mStart);
            mDesc.setText(d);
            mNextDate.setText("(" + l.mNext.mStart + ")");
            mNextTitle.setText(l.mNext.mTitle);
            mTop.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    LiveHolder h = (LiveHolder) v.getTag();
                    LOGd(TAG, "Clicked this " + h.mTitle.getText());
                    if (mListener == null) return;
                    mListener.onSelect(mData);
                }
            });
        }
    }
}
// EOF
